﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
	internal class ChangeElementOrder : Operation
	{
        public enum Direction
        {
            Up,
            Down,
            Front,
        }

        Direction dir;
        Engine.Selection Selected;
        
        List<Jemgine.Data.EntityElement> StoredList;
        Data.EntityRecord Entity;

        internal ChangeElementOrder(Engine.Selection Selected, Direction dir)
		{
			this.Selected = Selected;
            this.dir = dir;
		}

		internal override ReturnCode Do(EditorState ES)
		{
            Entity = ES.GetTopObject(Selected) as Data.EntityRecord;
            if (Entity == null) return ReturnCode.DoNotAddToUndoStack;

            if (Selected.MiddleIndex == 0 && (dir == Direction.Down || dir == Direction.Front))
                return ReturnCode.DoNotAddToUndoStack;
            if (Selected.MiddleIndex == Entity.AttachedElements.Count - 1 && dir == Direction.Up)
                return ReturnCode.DoNotAddToUndoStack;

            StoredList = new List<Jemgine.Data.EntityElement>(Entity.AttachedElements);
            int NewIndex = 0;

            switch (dir)
            {
                case Direction.Down:
                    {
                        var Temp = Entity.AttachedElements[Selected.MiddleIndex];
                        Entity.AttachedElements[Selected.MiddleIndex] = Entity.AttachedElements[Selected.MiddleIndex - 1];
                        Entity.AttachedElements[Selected.MiddleIndex - 1] = Temp;
                        NewIndex = Selected.MiddleIndex - 1;
                    }
                    break;
                case Direction.Up:
                    {
                        var Temp = Entity.AttachedElements[Selected.MiddleIndex];
                        Entity.AttachedElements[Selected.MiddleIndex] = Entity.AttachedElements[Selected.MiddleIndex + 1];
                        Entity.AttachedElements[Selected.MiddleIndex + 1] = Temp;
                        NewIndex = Selected.MiddleIndex + 1;
                    }
                    break;
                case Direction.Front:
                    {
                        var Temp = Entity.AttachedElements[Selected.MiddleIndex];
                        Entity.AttachedElements.RemoveAt(Selected.MiddleIndex);
                        Entity.AttachedElements.Insert(0, Temp);
                        NewIndex = 0;
                    }
                    break;
            }

            ES.RebuildNode(Selected.TopIndex);
            ES.SelectItem(new Jemgine.Engine.Selection(Selected.Type, Selected.TopIndex, 
                NewIndex, Selected.BottomIndex));

			return ReturnCode.AddToUndoStack;
		}

        internal override void Undo(EditorState ES)
        {
            ES.RebuildNode(Selected.TopIndex);
            Entity.AttachedElements.Clear();
            Entity.AttachedElements.AddRange(StoredList);
            ES.SelectItem(ES.Selected);
        }
	}
}
